package api

import (
	"errors"
	"github.com/gin-gonic/gin"
	"github.com/smash-llj/Nexa-server/models/request"
	service "github.com/smash-llj/Nexa-server/service"
	utils "github.com/smash-llj/Nexa-server/utils"
	"strconv"
)

type FriendApi struct {
	BaseApi
	Service *service.FriendService
}

func NewFriendApi() FriendApi {
	return FriendApi{
		BaseApi: NewBaseApi(),
		Service: service.NewFriendService(),
	}
}

// GetFriendList 获取好友列表
func (f *FriendApi) GetFriendList(C *gin.Context) {
	f.BaseApi.C = C
	uid := utils.GetUserId(C)
	list, err := f.Service.GetFriendList(uid)
	if err != nil {
		f.Fail(Response{Msg: err.Error()})
		return
	}
	f.Success(Response{Data: list})
}

// GetFriendMessageList 获取好友消息
func (f *FriendApi) GetFriendMessageList(C *gin.Context) {
	var params request.GetFriendMessageReq
	userId := utils.GetUserId(C)
	if err := f.BindData(BuildBaseApiOptions{C: C, Dto: &params}); err != nil {
		f.Fail(Response{Msg: err.Error()})
		return
	}
	list, err := f.Service.GetFriendMessageList(params, userId)
	if err != nil {
		f.Fail(Response{Msg: err.Error()})
		return
	}
	f.Success(Response{Data: list})
}

// SearchFriend 搜索好友
func (f *FriendApi) SearchFriend(C *gin.Context) {
	var params request.SearchFriendReq
	if err := f.BindData(BuildBaseApiOptions{C: C, Dto: &params}); err != nil {
		f.Fail(Response{Msg: err.Error()})
		return
	}
	list, err := f.Service.SearchFriend(params)
	if err != nil {
		f.Fail(Response{Msg: err.Error()})
		return
	}
	f.Success(Response{Data: list})
}

// AddFriend 添加好友
func (f *FriendApi) AddFriend(C *gin.Context) {
	var userDto request.AddFriendReq
	if err := f.BindData(BuildBaseApiOptions{C: C, Dto: &userDto}); err != nil {
		f.Fail(Response{Msg: err.Error()})
		return
	}
	uid := utils.GetUserId(C)
	if err := f.Service.AddFriend(userDto, int(uid)); err != nil {
		f.Fail(Response{Msg: err.Error()})
		return
	}
	f.Success(Response{
		Data: "添加好友成功",
	})
}

// RequestFriendList 获取好友请求列表
func (f *FriendApi) RequestFriendList(C *gin.Context) {
	f.BaseApi.C = C
	uid := utils.GetUserId(C)
	requestList, err := f.Service.RequestFriendList(int(uid))
	if err != nil {
		f.Fail(Response{Msg: err.Error()})
		return
	}
	f.Success(Response{Msg: "ok", Data: requestList})
}

// AgreeFriend 同意好友
func (f *FriendApi) AgreeFriend(C *gin.Context) {
	var userDto request.UserFriendReq
	if err := f.BindData(BuildBaseApiOptions{C: C, Dto: &userDto}); err != nil {
		f.Fail(Response{Msg: err.Error()})
		return
	}
	result, err := f.Service.AgreeFriend(userDto)
	if err != nil {
		f.Fail(Response{Msg: "出现错误"})
		return
	}
	f.Success(Response{Msg: "ok", Data: result})
}

// DelFriend 删除好友
func (f *FriendApi) DelFriend(C *gin.Context) {
	var userDto request.UserFriendReq
	if err := f.BindData(BuildBaseApiOptions{C: C, Dto: &userDto}); err != nil {
		f.Fail(Response{Msg: err.Error()})
		return
	}
	if err := f.Service.DelFriend(userDto); err != nil {
		f.Fail(Response{Msg: err.Error()})
		return
	}
	f.Success(Response{
		Msg: "删除好友成功",
	})
}

// DetailFriend 获取好友详情
func (f *FriendApi) DetailFriend(C *gin.Context) {
	f.BaseApi.C = C
	FId := C.Param("friendId")
	userId := utils.GetUserId(C)
	if FId == "" {
		f.Fail(Response{Msg: errors.New("friendId不能为空")})
	}
	friendId, _ := strconv.Atoi(FId)
	friend, err := f.Service.DetailFriend(uint(friendId), userId)
	if err != nil {
		f.Fail(Response{Msg: errors.New("获取失败")})
		return
	}
	f.Success(Response{Msg: "ok", Data: friend})
}
